package com.fbox;
import java.io.IOException;
import java.text.MessageFormat;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fbox.models.BoxGroup;
import com.fbox.models.BoxReg;

public class Main {
	
	private static  ConsoleLoggerFactory loggerFactory = new ConsoleLoggerFactory();
	private static  TokenManager tokenManager = new TokenManager(new StaticCredentialProvider(Global.clientId, Global.clientSecret, Global.username, Global.password), Global.idServerUrl, loggerFactory);
	 
    private static ServerCaller commServer = new ServerCaller(tokenManager, Global.commServerApiUrl, Global.signalrClientId, loggerFactory);
    private  static ServerCaller appServer = new ServerCaller(tokenManager, Global.appServerApiUrl, Global.signalrClientId, loggerFactory);
    
    private  static ServerCaller hsserver = new ServerCaller(tokenManager, Global.hsServer, Global.signalrClientId, loggerFactory);

    static{
    	Global.commServer = commServer;
        Global.appServer = appServer;
        Global.hsserver = hsserver;
    }
     
	
    public static JSONObject getBoxInfo(String boxNo){
    	JSONObject json = new JSONObject();
    	//String url = "beginTime=0&endTime="+System.currentTimeMillis()+"&limit=500";
        String str = null;
		try {
			str = Global.appServer.executeGet(MessageFormat.format("box/reg/boxno/{0}", boxNo),String.class);
		} catch (Exception e) {
			
			e.printStackTrace();
			return null;
		}
        System.out.println("888888"+str);
       // json.put("result", str);
        return  JSONObject.parseObject(str);
    }
	
    public static JSONArray getAlarmRecord(String boxId,long begintime,long endTime)throws Exception{
 
    	String url = "beginTime="+begintime+"&endTime="+endTime+"&limit=500";
       String str  = Global.commServer.executeGet(MessageFormat.format("v2/box/{0}/alarm/data?{1}",boxId,url),String.class);

        System.out.println("888888"+str);
        return  JSONArray.parseArray(str);
    }
    
    public static JSONArray getAlarmRecord(String boxId)throws Exception{
    	 
    
        return  getAlarmRecord(boxId, 0L,System.currentTimeMillis());
    }
    
    public static JSONArray hdataitems(String boxId)throws Exception{
    	
       String str  = Global.commServer.executeGet(MessageFormat.format("v2/box/{0}/hdataitems",boxId),String.class);

        System.out.println("888888"+str);
        return  JSONArray.parseArray(str);//JSONObject.parseObject(str);
    }
    
    public static JSONObject hdata()throws Exception{
    	long newSecond = LocalDateTime.now().plusHours(-1).withMinute(0).withSecond(0).withNano(0)
    			.toInstant(ZoneOffset.of("+8")).toEpochMilli();
    	Map<String,Object> map =new HashMap<>();
    	Long[] ids = new Long[]{95630600737756780L};
    	return hdata(ids, newSecond, System.currentTimeMillis(), 1, 0, 500);
   
       
     }
    
    public static JSONObject hdata(Long[] ids)throws Exception{
    	long newSecond = LocalDateTime.now().plusDays(-7).withHour(0).withMinute(0).withSecond(0).withNano(0)
    			.toInstant(ZoneOffset.of("+8")).toEpochMilli();
    	Map<String,Object> map =new HashMap<>();
    	//long[] ids = new long[]{95630600737756780L};
    	return hdata(ids, newSecond, System.currentTimeMillis(), 1, 0, 500);
   
       
     }
    
    public static JSONArray monitor(String boxId) throws Exception{
    	 String str  =  Global.commServer.executeGet("v2/box/"+boxId+"/dmon/grouped",String.class);
         
         System.out.println("888888"+str);
        return  JSONArray.parseArray(str);
    }

    public static JSONArray monitorList(String boxId) throws Exception{
    	JSONArray json = monitor(boxId)  ; 
    	System.out.println(json);
    	JSONArray array = new JSONArray();
    	if(json != null){
    		for (int j = 0; j < json.size(); j++) {
    			array.addAll(json.getJSONObject(j).getJSONArray("items"));
			}
    	}
    	System.out.println();
    	return array;
        
   }
    
    public static JSONObject hdata(Long[] ids,long begin,long end,int g,int type,int limit )throws Exception{
    	
    	Map<String,Object> map =new HashMap<>();
    	//long[] ids = new long[]{95630600737756780L};
    /*    map.put("beginTime", "0");95630600737756781L,95630600722028139L,95630600722028138L,
        map.put("endTime", System.currentTimeMillis()+"");*/
        map.put("type", type);// 0/1
        map.put("format", "0");
        map.put("ids", ids);
        map.put("g", g);
        map.put("begin", begin);
        map.put("end", end);
        map.put("tr", 3);
        map.put("limit", limit);
        map.put("tz", "");

        
        String str  =  Global.hsserver.executePost("v2/hdata/get",map,String.class);
        
         System.out.println("888888"+str);
        // json.put("result", str);
        return  JSONObject.parseObject(str);
         
     }
   /* public static void main(String[] args) {
    	main();
	}*/
    public static void main(){
    	try {
			hdataitems("12633");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	 /*FBoxSignalRConnection fboxSignalR = new FBoxSignalRConnection(Global.commServerSignalRUrl, Global.signalrClientId, tokenManager, Global.proxy, loggerFactory);

         fboxSignalR.connected(() -> {
         });
         fboxSignalR.start();*/

    }
    
  /*  public static void main(String[] args) {
        ConsoleLoggerFactory loggerFactory = new ConsoleLoggerFactory();
       TokenManager tokenManager = new TokenManager(new StaticCredentialProvider(Global.clientId, Global.clientSecret, Global.username, Global.password), Global.idServerUrl, loggerFactory);
 
        ServerCaller commServer = new ServerCaller(tokenManager, Global.commServerApiUrl, Global.signalrClientId, loggerFactory);
        ServerCaller appServer = new ServerCaller(tokenManager, Global.appServerApiUrl, Global.signalrClientId, loggerFactory);

        Global.commServer = commServer;
        Global.appServer = appServer;

        FBoxSignalRConnection fboxSignalR = new FBoxSignalRConnection(Global.commServerSignalRUrl, Global.signalrClientId, tokenManager, Global.proxy, loggerFactory);

        fboxSignalR.connected(() -> {
        });
        fboxSignalR.start();

        System.out.println("Box list:");
        try {
            BoxGroup[] boxGroups = Global.appServer.executeGet("box/grouped", BoxGroup[].class);

            for (BoxGroup group : boxGroups) {
                for (BoxReg boxReg : group.boxRegs) {
                	//System.out.println(boxReg);
                	System.out.println(JSONObject.toJSON(boxReg));
                   // System.out.printf("\t%s\t%s\t%s\n", boxReg.alias, boxReg.box.boxNo, boxReg.box.boxType);
                }
            }
            
            String str  = Global.commServer.executeGet("v2/box/alarm?boxNo=300618010026", String.class);
            System.out.println("888888"+str);
            Map<String,String> map =new HashMap<String, String>();
            map.put("beginTime", "0");
            map.put("endTime", System.currentTimeMillis()+"");
            map.put("limit", "500");
            String url = "beginTime=0&endTime="+System.currentTimeMillis()+"&limit=500";
            str  = Global.commServer.executeGet("v2/box/13547/alarm/data?"+url,String.class);
            System.out.println("888888"+str);
           System.out.println(getBoxInfo("300618010026")); ;
           System.out.println(getAlarmRecord("13588"));
           System.out.println(hdataitems("13576"));
           System.out.println(hdata());
        } catch (Exception e) {
            e.printStackTrace();
        }


        Scanner s = new Scanner(System.in);
        s.nextLine();
    }*/
}
